import javax.swing.*;
import javax.swing.plaf.FontUIResource;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Objects;

public class WindowTwoListener implements ActionListener {
    WindowTwo windowTwo;
    int i = 0;
    int j = 4;

    public void setWindowTwo(WindowTwo windowTwo){
        this.windowTwo =windowTwo;
    }
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == windowTwo.button) {
            if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(0).getSheetName())) {
                //选择班级，改变姓名
                String[] name = new String[3];
                int k = 4;
                for (int i = 0; i < 3; i++) {
                    name[i] = String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(k).getCell(1));
                    k++;
                }
                windowTwo.comboBox3 = new JComboBox<>(name);
                windowTwo.comboBox3.setBackground(Color.white);
                windowTwo.comboBox3.setPreferredSize(new Dimension(220, 22));
                windowTwo.comboBox3.setSelectedIndex(0);
                windowTwo.boxFour.remove(1);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(4).getCell(3)));
                //更改后的表单不可监听。需重新监听
                windowTwo.boxFour.add(windowTwo.comboBox3);
                windowTwo.comboBox3.addActionListener(this);
                //按钮可点击或不可点击
                windowTwo.button2.setEnabled(true);
                windowTwo.button3.setEnabled(false);
            } else if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(1).getSheetName())) {
                String[] name = new String[3];
                int k = 4;
                for (int i = 0; i < 3; i++) {
                    name[i] = String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(k).getCell(1));
                    k++;
                }
                windowTwo.comboBox3 = new JComboBox<>(name);
                windowTwo.comboBox3.setBackground(Color.white);
                windowTwo.comboBox3.setPreferredSize(new Dimension(220, 22));
                windowTwo.comboBox3.setSelectedIndex(0);
                windowTwo.boxFour.remove(1);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(4).getCell(3)));
                //更改后的表单不可监听。需重新监听
                windowTwo.boxFour.add(windowTwo.comboBox3);
                windowTwo.comboBox3.addActionListener(this);
                //按钮可点击或不可点击
                windowTwo.button2.setEnabled(true);
                windowTwo.button3.setEnabled(false);
            }
        }
        //下一个，学号跟姓名关联
        if (e.getSource() == windowTwo.button2) {
            windowTwo.button3.setEnabled(true);
            if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(0).getSheetName())) {
                if (Objects.equals(WindowTwo.jTextField.getText(), "202210098100")) {
                    i = 0;
                    j = 4;
                }
                i++;
                j++;
                windowTwo.comboBox3.setSelectedIndex(i);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(j).getCell(3)));//设置学号
                windowTwo.jTextField1.setText("");//清空分数
                //设置不可点击
                if(Objects.equals(WindowTwo.jTextField.getText(),String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(6).getCell(3)))){
                    windowTwo.button2.setEnabled(false);
                }
            } else if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(1).getSheetName())) {
                if (Objects.equals(WindowTwo.jTextField.getText(), "202110098200")) {
                    i = 0;
                    j = 4;
                }
                i++;
                j++;
                windowTwo.comboBox3.setSelectedIndex(i);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(j).getCell(3)));//设置学号
                windowTwo.jTextField1.setText("");//清空分数
                //设置不可点击
                if(Objects.equals(WindowTwo.jTextField.getText(),String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(6).getCell(3)))){
                    windowTwo.button2.setEnabled(false);
                }
            }
        }
        //上一个，学号跟姓名关联
        if (e.getSource() == windowTwo.button3) {
            windowTwo.button2.setEnabled(true);
            if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(0).getSheetName())) {
                i--;
                j--;
                windowTwo.comboBox3.setSelectedIndex(i);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(j).getCell(3)));
                windowTwo.jTextField1.setText("");
                //设置不可点击
                if(Objects.equals(WindowTwo.jTextField.getText(),String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(4).getCell(3)))){
                    windowTwo.button3.setEnabled(false);
                }
            } else if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(1).getSheetName())) {
                i--;
                j--;
                windowTwo.comboBox3.setSelectedIndex(i);
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(j).getCell(3)));
                windowTwo.jTextField1.setText("");
                //设置不可点击
                if(Objects.equals(WindowTwo.jTextField.getText(),String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(4).getCell(3)))){
                    windowTwo.button3.setEnabled(false);
                }
            }
        }

        //监听姓名表单
        if(e.getSource() == windowTwo.comboBox3) {
            i = windowTwo.comboBox3.getSelectedIndex();
            j = windowTwo.comboBox3.getSelectedIndex() + 4;
            //使按钮可电机或不可点击
            if(Objects.equals(windowTwo.comboBox3.getSelectedIndex(),0)){
                windowTwo.button2.setEnabled(true);
                windowTwo.button3.setEnabled(false);
            }else if(Objects.equals(windowTwo.comboBox3.getSelectedIndex(),2)){
                windowTwo.button2.setEnabled(false);
                windowTwo.button3.setEnabled(true);
            }else if(Objects.equals(windowTwo.comboBox3.getSelectedIndex(),1)){
                windowTwo.button2.setEnabled(true);
                windowTwo.button3.setEnabled(true);
            }

            //更改姓名表单，跟学号关联
            if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(0).getSheetName())) {
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(0).getRow(windowTwo.comboBox3.getSelectedIndex() + 4).getCell(3)));
            }else if (Objects.equals(windowTwo.comboBox1.getSelectedItem(), windowTwo.workbook.getSheetAt(1).getSheetName())) {
                WindowTwo.jTextField.setText(String.valueOf(windowTwo.workbook.getSheetAt(1).getRow(windowTwo.comboBox3.getSelectedIndex() + 4).getCell(3)));
            }
        }
    }
}
